package com.lanou.zhihu.web.controller;

import com.lanou.zhihu.entity.Question;
import com.lanou.zhihu.entity.User;
import com.lanou.zhihu.service.QuestionService;
import com.sun.jersey.api.client.Client;
import com.sun.jersey.api.client.WebResource;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.http.HttpServletRequest;
import java.io.IOException;
import java.io.InputStream;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * Created by QuChao on 2018/10/25.
 */
@Controller
@RequestMapping("/question")
public class QuestionController {
    @Autowired
    private QuestionService questionService;

    /*上传图片使用的是跨服务器上传*/
    @ResponseBody
    @RequestMapping("/add")
    public Map<String, Object> add(Question question, HttpServletRequest request, MultipartFile image) throws IOException {
        InputStream inputStream = image.getInputStream();
        String newName = new Date().getTime() + "question.jpg";
        /*这个是上传的真实路径，可以自行修改*/
        String filePath = "http://47.106.73.53/zhihu/UserImage/" + newName;
        /*这个是夸服务器上传调用的方法，如果不使用跨服务器上传，可以直接调用FileUtil工具类*/
        Client client = Client.create();
        WebResource resource = client.resource(filePath);
        resource.put(inputStream);

        User user = (User) request.getSession().getAttribute("user");
        question.setUid(user.getId());
        question.setImage(filePath);
        Map<String, Object> map = questionService.add(question);
        return map;
    }

    @ResponseBody
    @RequestMapping("/list")
    public Map<String, Object> list() {
        Map<String, Object> map = new HashMap<>();
        List<Question> questionList = questionService.findAll();
        map.put("rows", questionList);
        return map;
    }

    @ResponseBody
    @RequestMapping("/findById")
    public Map<String, Object> findById(Integer id) {
        Map<String, Object> map = new HashMap<>();
        Question question = questionService.findById(id);
        map.put("rows", question);
        return map;
    }

    @ResponseBody
    @RequestMapping("/findByTid")
    public Map<String, Object> findByTid(Integer tid) {
        Map<String, Object> map = new HashMap<>();
        List<Question> questionList = questionService.findByTid(tid);
        map.put("rows", questionList);
        return map;
    }

    /*summernote插件上传图片的接口，返回值是上传路径*/
    @ResponseBody
    @RequestMapping("/uploadImage")
    public String uploadImage(MultipartFile file) throws IOException {
        InputStream inputStream = file.getInputStream();
        String newName = new Date().getTime() + "QuestionModel.jpg";
        Client client = Client.create();
        String filePath = "http://47.106.73.53/zhihu/UserImage/" + newName;
        WebResource resource = client.resource(filePath);
        resource.put(inputStream);
        return filePath;
    }
}
